package uk.gov.swmprobation.swm;

import java.util.ArrayList;

import uk.gov.swmprobation.swm.RssParser.RssFeed;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class News extends Activity {
 
ProgressBar progressBar;
 ListView list;
 ArrayList<String> titles=new ArrayList<String>();
 ArrayList<String> links=new ArrayList<String>();
 ArrayList<String> listItems=new ArrayList<String>();
 ArrayAdapter<String> adapter;
 int clickCounter=0;

 
 public class BackgroundAsyncTask extends
    AsyncTask<Void, Integer, String> {
  
  int myProgress;

  @Override
  protected void onPostExecute(String result) { 
   // TODO Auto-generated method stub
	  if (RssParser.test) {
	  for (int i = 0; i < titles.size(); i++) {
		  listItems.add(titles.get(i));
	  }
	  adapter.notifyDataSetChanged();
	  } else {
		  Toast.makeText(News.this, "Sorry, News can not be contacted at the moment, please try again later", Toast.LENGTH_LONG).show();
		  finish();
	  }
	  progressBar.setVisibility(View.GONE);
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   myProgress = 0;
  }

  @Override
  protected String doInBackground(Void... params) {

	  RssParser rp = new RssParser("http://www.swmprobation.gov.uk/?feed=rss2&tag=mobile"); 
	  rp.parse();
	  if (RssParser.test){
	  RssFeed feed = rp.getFeed();
	  
	  /* Listing all items in the feed */
	  for (int i = 0; i < Math.min(feed.items.size(),5); i++) {
		  titles.add (feed.items.get(i).title);
		  links.add (feed.items.get(i).link);
		  /*System.out.println(feed.items.get(i).title +":"+feed.items.get(i).link);*/
	   publishProgress(100*(i+1)/feed.items.size());
	  }
	  }
	  
   // TODO Auto-generated method stub

   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   // TODO Auto-generated method stub
   progressBar.setProgress(values[0]);
  }

 }
 
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     setContentView(R.layout.activity_news);
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
  
     progressBar = (ProgressBar)findViewById(R.id.progressbar);
     progressBar.setProgress(0);
     list = (ListView)findViewById(R.id.listHere);
     
     adapter=new ArrayAdapter<String>(this,
             R.layout.news_item,
             listItems);
         list.setAdapter(adapter);;
         list.setOnItemClickListener(new OnItemClickListener(){
             public void onItemClick(AdapterView<?> parent, View v, int position, long id){
            	 String rssUrl = links.get(position);
            	 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssUrl));
            	 startActivity(browserIntent);
             }
         }
        );


         
     new BackgroundAsyncTask().execute();
 }
 
}

